package com.zekdot.password.service.impl;

import com.zekdot.password.config.ConfigInfo;
import com.zekdot.password.dao.UserDao;
import com.zekdot.password.dto.Envelope;
import com.zekdot.password.entity.UserEntity;
import com.zekdot.password.service.UserService;
import com.zekdot.password.util.PasswordUtil;
import org.apache.commons.lang.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Create by zekdot on 19-12-28.
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;
    @Autowired
    private ConfigInfo configInfo;

    @Override
    public Envelope login(UserEntity userEntity) {
        // 如果没有配置多用户，直接用默认用户完成登录
        if (!configInfo.multiuser) {
            if (configInfo.defaultuser == null) {
                return new Envelope(-1, "未配置默认用户，请先完成配置！", null);
            }
            userEntity.setUsername(configInfo.defaultuser);
            return new Envelope(userEntity);
        }
        UserEntity userEntity1 = userDao.getUserByUsername(userEntity.getUsername());
        if(userEntity1 == null){

            return new Envelope(1,"用户不存在",null);
        }
        if(!userEntity1.getPassword().equals(PasswordUtil.getMd5(userEntity.getUsername(),
                userEntity.getPassword()))){
            return new Envelope(1,"用户名或密码错误",null);
        }
        return new Envelope(userEntity);
    }
}
